package com.hai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hai.exception.BusinessException;
import com.hai.response.ResultCode;
import com.hai.entity.User;
import com.hai.service.UserService;
import com.hai.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hai.response.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author NieChangan
 * @since 2022-05-21
 */
@Slf4j
@Api(value = "系统用户模块", tags="系统用户接口")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "查询所有用户", notes = "xxxx")
    @GetMapping("/getUsers")
    public Result getUsers(){

        List<User> userList = userService.list();

        return Result.ok().data("users",userList);
    }

    /**
     * 查询单个用户
     * @return
     */
    @ApiOperation(value = "用户登录" , notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @GetMapping("login")
    public Result login(@NotBlank(message = "账号必填") String username,
                        @NotBlank(message = "密码必填") String password,
                        HttpServletRequest request){
        User user = userService.findUserByName(username);
        if (user==null){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(), ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }

        return Result.ok().data("user",user);
    }

    /**
     * 分页+条件+连接查询用户信息
     * @param current
     * @param size
     * @param userVO
     * @return
     */
    @ApiOperation(value = "分页+条件+连接查询用户信息" , notes = "xxxx")
    @PostMapping ("getUserByPage")
    public Result getUserByPage(@RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
                                @RequestParam(value = "size",required = true,defaultValue = "7") Integer size,
                                @RequestBody UserVO userVO){

        Page<User> page = new Page<>(current,size);
        Page<User> userPage = userService.getUserByPage(page, userVO);
        return Result.ok().data("userPage",userPage);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "修改用户状态" , notes = "xxxx")
    @PutMapping("updateUserStatus")
    public Result updateUserStatus(@RequestParam(value = "id") Long id ,
                                   @RequestParam(value = "status") Integer status){

        User user = new User();
        user.setId(id);
        user.setStatus(status);
        boolean b = userService.updateById(user);

        if (b) return Result.ok();

        return Result.error(ResultCode.COMMON_FAIL);
    }


    /**
     * 逻辑删除用户
     * @param id
     * @return
     */
    @DeleteMapping("deleteUser")
    public Result deleteUser(@RequestParam(value = "id") Long id){
        boolean b = userService.removeById(id);

        if (b) return Result.ok();

        return Result.error(ResultCode.COMMON_FAIL);
    }


    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {

        //根据条件进行查询
        List<User> users = userService.getUserListToExcel(userVO);


        userService.exportUserExcel(response,users);

    }






}
